package com.ym.service.impl;


import com.ym.domain.Build;
import com.ym.domain.Floor;
import com.ym.dto.FloorDto;
import com.ym.mapper.BuildMapper;
import com.ym.service.IBuildService;
import com.ym.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuildServiceimpl implements IBuildService {

    @Autowired
    private BuildMapper buildMapper;


    @Override
    public JSONResult addFloor(List<FloorDto> list) {
        String a = "";
        for (FloorDto dto : list) {
            Build build = buildMapper.selectById(dto.getBuildId());
            if (build.getId() == null){
                a = a + "楼栋不存在";
            }
            if (dto.getBuildId() == null){
                a = a + "楼栋id不能为空 ";
            }
            if (dto.getName() == null || "".equals(dto.getName())){
                a = a + "楼层不能为空";
            }
            if(dto.getBuildId() == null || dto.getName() == null ||
                    "".equals(dto.getName())){
                return JSONResult.error(a);
            }
            Floor floor = new Floor();
            floor.setBuildId(dto.getBuildId());
            floor.setName(dto.getName());
            buildMapper.addFloor(floor);
        }
        return new JSONResult();
    }

    @Override
    public Build findByid(Long id) {
        return buildMapper.selectById(id);
    }

    @Override
    public List<Floor> getFloorListByBuildId(Long id) {
        return buildMapper.getFloorListByBuildId(id);
    }
}
